package se.vgregion.icc;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/***
 * A class that have help methods for PingForConfiguration project.
 * 
 * @author andan40 & ricra2
 *
 */
public final class PingForConfigurationUtils {

    /** Class logger. */
    private static final Logger LOG = LoggerFactory.getLogger(PingForConfigurationUtils.class);

    private PingForConfigurationUtils() {
    }

    /***
     * Gets the text from the file path.
     * 
     * @param filePath to file.
     * @return text in file.
     * @throws IOException - if an I/O error occurs
     */
    public static String getTextFromFile(String filePath) throws IOException {
        LOG.debug("Getting the files content as a String from filepath: {}", filePath);
        return IOUtils.toString(new FileReader(new File(filePath)));
    }

    /***
     * Checks if the object you have exists in the Object[].
     * 
     * @param toFind Object you wish to find in list.
     * @param array The list where you expect the object to exist.
     * @return true if the Object is found.
     */
    public static boolean doesObjectExistInArray(Object toFind, Object[] array) {
        boolean exists = false;

        if (array == null) {
            throw new NullPointerException("Array is null");
        }

        for (Object element : array) {
            if (element.equals(toFind)) {
                LOG.debug("Object {} exsist in array {}", toFind, array);
                exists = true;
            }
        }
        return exists;
    }

    /***
     * Restore escaped xml tags to real tags.
     * 
     * @param xmlString String to restore
     * @return Restored string
     */
    public static String restoreXmlTags(String xmlString) {
        while (xmlString.contains("lt;")) {
            xmlString = StringEscapeUtils.unescapeXml(xmlString);
        }
        return xmlString;
    }
}
